package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import model.Follow;

import org.apache.log4j.Logger;

import common.StartupServlet;

public class FollowDao extends RpDao {
	private static Logger log = Logger.getLogger(Follow.class);

	public int insert(Follow fo) {
		int n = 0;
		Connection conn=this.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			//insertFollow = "insert into follow(uid,itemid)values(?,?)",
			pstmt = conn.prepareStatement(SqlString.insertFollow, Statement.RETURN_GENERATED_KEYS);
			pstmt.setInt(1, fo.getUid());
			pstmt.setInt(2, fo.getItemid());

			n = pstmt.executeUpdate();
			if (1 == n) {
				rs = pstmt.getGeneratedKeys();
				rs.next();
				n = rs.getInt(1);// 成功则返回 id
			}
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			n = -1;
		} finally {
			this.closeDao(conn, pstmt, rs);
		}
		return n;
	}

	public static void main(String[] args) {
		new StartupServlet();
		FollowDao fdao = new FollowDao();
		Follow fo = new Follow();
		fo.setUid(11);
		fo.setItemid(21);
		System.out.println(fdao.insert(fo));
	}
}
